package com.gem.back.controller;


import com.gem.back.entity.AoaUser;
import com.gem.back.service.IAoaNoticeListService;
import com.gem.back.service.IAoaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hhhy
 * @since 2020-02-22
 */
@Controller
@RequestMapping("/a/user")
public class AoaUserController {
    @Autowired
    private IAoaUserService aoaUserService;
    @Autowired
    private IAoaNoticeListService aoaNoticeListService;

    //登录
    @RequestMapping("/login")
    public String login(){
        return "login/login";
    }

    //退出登录
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null){
            new SecurityContextLogoutHandler().logout(request,response,authentication);
        }
        return "redirect:/a/user/login";
    }


    //用户管理
    @RequestMapping("/usermanage")
    public String userManage(Integer userId, Model model){
        List<AoaUser> userList = aoaUserService.list();
        model.addAttribute("users",userList);
        return "user/usermanage";
    }

    //个人中心
    @RequestMapping("/userpanel")
    public String userPanel(Model model, @SessionAttribute("userId") Integer userId){
        AoaUser aoaUser = aoaUserService.findUserByUserId(userId);
        //通知列表数
        Integer noticeCount = aoaNoticeListService.getMyTotal("",userId);
        model.addAttribute("aoaUser",aoaUser);
        model.addAttribute("noticeCount",noticeCount);
        return "user/userpanel";
    }

    //修改密码
    @RequestMapping("/modifyPwd")
    public String modifyPwd(Model model,@SessionAttribute("userId") Integer userId){
        AoaUser aoaUser = aoaUserService.findUserByUserId(userId);
        model.addAttribute("aoaUser",aoaUser);
        return "user/modifyUserPwd";
    }

    //保存新密码
    @ResponseBody
    @RequestMapping("/modifySubmit")
    public String modifySubmit(AoaUser aoaUser){
        System.out.println("==========================================================>" + aoaUser);
        //注意此时提交的密码时明文，需要转成密文
        String pwd = new BCryptPasswordEncoder().encode(aoaUser.getPassword());
        aoaUser.setPassword(pwd);
        System.out.println("============================================>" + pwd);
        aoaUserService.saveOrUpdate(aoaUser);
        return "success";
    }

    //保存我的信息
    @RequestMapping("/saveUser")
    public String saveUser(AoaUser aoaUser){
        aoaUserService.saveOrUpdate(aoaUser);
        return "redirect:userpanel";
    }



}
